#!/bin/zsh
#normalize_aarr() {
  emulate -L zsh; setopt local_options
  local MATCH MBEGIN MEND; local -a match mbegin mend  # appease 'warn_create_global'

  local input key value array_name
  local -A assoc_array

  # Read from standard input
  while IFS= read -r input; do
    if [[ $input =~ '^typeset -A ([a-zA-Z_][a-zA-Z0-9_]*)=\((.*)\)$' ]]; then
      array_name=${match[1]}
      eval "assoc_array=( ${match[2]} )"

      # Print in the new format
      echo -n "typeset -A $array_name=("
      for key in "${(@ok)assoc_array}"; do
        value=${assoc_array[$key]}
        quoted_value=${(q)value}  # Apply (q) to check if quoting is needed

        # Use quoted_value only if it differs from value
        [[ $quoted_value != "$value" ]] && value=${(qq)value}

        echo -n " [$key]=$value"
      done
      echo " )"
    else
      echo "Error: Input does not match an associative array declaration" >&2
      return 1
    fi
  done
#}
